var chukutypes = [];

var search_data = {page:1,limit:5,
    status:stitle1.value,
    start_date:start_date.value,
    end_date:end_date.value};
loadData();

function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        "width='100%'><tr class='theader'>" +
        "<th>出库行为</th><th>创建时间</th><th>状态</th>" +
        "<th>操作</th></tr>";
    for(var i=arr.length-1;i>=0;i--){
        var a = arr[i];
        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.chuku_behavior+"</td>" +
            "<td class='ntitle'>"+a.build_time+"</td>" +
            "<td class='ntitle'>"+a.status+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd1("+a.id+")' href='javascript:;'>启用</a>" +
            "<a class='edit_a' onclick='upd2("+a.id+")' href='javascript:;'>禁用</a></td>" +
            // "<a class='del_a' onclick='del("+a.id+")' href='javascript:;'>禁用</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;
}

function search(){
    search_data={page:1,limit:5,status:stitle1.value,start_date:start_date.value,end_date:end_date.value}
    loadData();
}

function loadData(){
    list_div.innerHTML = "";
    loading.style.display = "block" ;
    ajax("/find_all_chukutype",search_data,function(data){
        chukutypes=data;
        showData(chukutypes);
        loading.style.display = "none" ;});

    ajax("/count_all_chukutype",search_data,function(data){
        // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data%search_data.limit==0?
                data/search_data.limit:
                Math.floor(data/search_data.limit)+1);
    });
}

function next_page(){
    if(search_data.page<parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData();
    }
}

function pre_page(){
    if(search_data.page>=2&&search_data.page<=parseInt(total_page.innerHTML)) {
        search_data.page--;
        loadData();
    }
}

function index_page(){
    search_data.page=1;
    loadData();
}

function tail_page(){
    search_data.page=parseInt(total_page.innerHTML);
    loadData();
}

function to_page(){
    var ip = parseInt(input_page.value);
    if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
        search_data.page = ip;
        loadData();
    }
}

function save(){
    ajax("/add_chukutype",{
        chuku_behavior:chuku_behavior.value,
        status:status1.value
    },function(data){
        alert("新增成功！");
        loadData();
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
        return_btn.style.display = "none";
        search_div.style.display = "block";
    })

}

function add(){
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";
}

function upd1(id){
    ajax("/upd_chukutype",{
        id:id,
        status:'启用'
    },function(data){
        loadData();
    });
}

function upd2(id){
    ajax("/upd_chukutype2",{
        id:id,
        status:'禁用'
    },function(data){
        loadData();
    });
}

function returnMain(){
    add_div.style.display = "none";
    list_div.style.display = "block";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
    search_div.style.display = "block";
    return_btn.style.display = "none";
}